package com.adam.zk.controller;

import com.adam.zk.controller.request.NewOrderRequest;
import com.adam.zk.model.Food;
import com.adam.zk.model.FoodOrder;
import com.adam.zk.service.FoodOrderService;
import com.adam.zk.service.FoodService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/order")
@Slf4j
public class FoodOrderController {
    @Autowired
    private FoodOrderService orderService;
    @Autowired
    private FoodService foodService;

    @GetMapping("/{id}")
    public FoodOrder getOrder(@PathVariable("id") Long id) {
        FoodOrder order = orderService.get(id);
        log.info("Get Order: {}", order);
        return order;
    }

    @PostMapping(path = "/", consumes = MediaType.APPLICATION_JSON_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseStatus(HttpStatus.CREATED)
    public FoodOrder create(@RequestBody NewOrderRequest newOrder) {
        log.info("Receive new Order {}", newOrder);
        Food[] foodList = foodService.getFoodByName(newOrder.getItems())
                .toArray(new Food[]{});
        return orderService.createOrder(newOrder.getConsumer(), foodList);
    }
}
